package ru.ipccenter.jshell.backend.client.ImportWidget;

import java.util.List;

import ru.ipccenter.jshell.backend.shared.ClassState;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ImportWidget extends Composite implements ClickHandler { 

  private final ImportWidgetUpdaterAsync importUpdater = GWT.create(ImportWidgetUpdater.class);
  private VerticalPanel nativeClassesPanel = new VerticalPanel();
  private VerticalPanel customClassesPanel = new VerticalPanel();
  
  public ImportWidget() {
    
    ScrollPanel mainScrollPanel = new ScrollPanel(nativeClassesPanel);
    mainScrollPanel.setSize("300px", "500px");
    
    DecoratorPanel mainDecoratorPanel = new DecoratorPanel();
    mainDecoratorPanel.add(mainScrollPanel);
        
    
    ScrollPanel customScrollPanel = new ScrollPanel(customClassesPanel);
    customScrollPanel.setSize("300px", "150px");
    
    DecoratorPanel customDecoratorPanel = new DecoratorPanel();
    customDecoratorPanel.add(customScrollPanel);
    
    VerticalPanel mainPanel = new VerticalPanel();
    mainPanel.setSpacing(2);
    
    mainPanel.add(mainDecoratorPanel);
    mainPanel.add(customDecoratorPanel);
    
    initWidget(mainPanel);
  }
  
  @SuppressWarnings("deprecation")
  @Override
  public void onClick(ClickEvent event) {
    
    CheckBox checkBox = (CheckBox) event.getSource();
    if (checkBox.isChecked()) {
      sendImportUpdate(ClassState.IMPORTED, checkBox.getText());
    } else {
      sendImportUpdate(ClassState.NOT_IMPORTED, checkBox.getText());
    }
  }
  
  public void uploadList() {
    importUpdater.getNativeClasses(new AsyncCallback<List<String>>() {
      @Override
      public void onFailure(Throwable arg0) {
        Window.alert("uploadClasses: importUpdater.getNativeClasses: error occured!");
      }
      
      @Override
      public void onSuccess(List<String> classes) {
        setNativeClasses(classes); 
      }
    });
    
    importUpdater.getCustomClasses(new AsyncCallback<List<String>>() {
      @Override
      public void onFailure(Throwable arg0) {
        Window.alert("uploadClasses: importUpdater.getCustomClasses: error occured!");
      }

      @Override
      public void onSuccess(List<String> classes) {
        setCustomClasses(classes); 
      }
    });
  }
  
  public void updateCustomList() {
    importUpdater.getCustomClasses(new AsyncCallback<List<String>>() {

      @Override
      public void onFailure(Throwable arg0) {
        Window.alert("updateCustomClasses: importUpdater.getCustomClasses: error occured!");
      }

      @Override
      public void onSuccess(List<String> classes) {
        clearCustomList();
        setCustomClasses(classes); 
      }
    });
  }
  
  protected void sendImportUpdate(ClassState state, String className) {
    importUpdater.update(state, className, new AsyncCallback<String>() {
      @Override
      public void onFailure(Throwable arg0) {
        Window.alert("importUpdater.update: error occured!");        
      }

      @Override
      public void onSuccess(String arg0) {}
    });
  }
  
  protected void setNativeClasses(List<String> classes) {
    setClasses(nativeClassesPanel, classes);
  }
  
  protected void setCustomClasses(List<String> classes) {
    setClasses(customClassesPanel, classes);
  }
  
  protected void setClasses(VerticalPanel panel, List<String> classes) {
    for (String className: classes) {
      CheckBox checkBox = new CheckBox(className);
      checkBox.addClickHandler(this);
      panel.add(checkBox);
    }
  }
  
  protected void clearCustomList() {
    customClassesPanel.clear();
  }
}
